package com.xuanwei.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.service.IXwUserCouponService;
import com.xuanwei.core.service.IXwUserService;
import com.xuanwei.dao.entity.XwUser;
import com.xuanwei.dao.entity.XwUserCoupon;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/7/19 10:05
 **/
@RestController
@RequestMapping("wx/user")
public class UserController extends BasicController {

    private IXwUserCouponService userCouponService;

    private IXwUserService userService;

    public UserController(IXwUserCouponService userCouponService, IXwUserService userService) {
        this.userCouponService = userCouponService;
        this.userService = userService;
    }

    @RequestMapping(value = "count", method = RequestMethod.GET)
    public ResultVO count() {
        XwUser user = userService.getOne(new QueryWrapper<XwUser>()
                .select("id,integral").eq("id", getUserId()));

        int count = userCouponService.count(new QueryWrapper<XwUserCoupon>()
                .eq("`status`", 0)
                .eq("user_id", getUserId())
                .ge("coupon_expire_time", LocalDateTime.now()));

        Map<String, Integer> result = new HashMap<>();
        result.put("integral", user.getIntegral());
        result.put("coupon", count);
        return ResultVO.successResult(result);
    }
}
